package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.MallLogisticsDto;
import com.huaxiafinance.management.dto.request.ScoreOrderDto;
import com.huaxiafinance.management.service.ScoreOrderService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by huangwencheng on 2017/9/4.
 * 查询积分订单列表，积分订单详情，修改物流信息，查询全部快递公司以及快递状态
 */

@Controller("ScoreOrderController")
@RequestMapping("/scoreOrder")
public class ScoreOrderController {

  private static final Logger log = Logger.getLogger(ScoreOrderController.class);

  @Autowired
  private ScoreOrderService scoreOrderService;

  /**
   * 请求查询积分订单列表
   *
   * @param dto 请求查询订单参数
   * @return 订单列表
   */
  @RequestMapping(value = "/queryScoreOrders", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryScoreOrders(@Valid @ModelAttribute("dto") ScoreOrderDto dto) {
    log.info("查询订单列表请求参数=======================>" + dto.toString());
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = scoreOrderService.queryScoreOrders(dto);
    } catch (Exception e) {
      log.info("异常原因=======================>" + e);
      messageResult.setFailureResult("系统异常");
    }

    log.info("查询订单列表返回参数=======================>" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求查询积分订单详情
   *
   * @param request 请求查询订单ID
   * @return 积分订单详情
   */
  @RequestMapping(value = "/scoreOrderDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String scoreOrderDetails(HttpServletRequest request) {
    log.info("查询订单详情请求参数=======================>" + request);
    MessageResult messageResult = new MessageResult();

    try {
      String id = request.getParameter("id");
      if (("").equals(id) || id == null) {
        messageResult.setFailureResult("系统异常,请重新点击");
        return messageResult.getAsJSON();
      }
      messageResult = scoreOrderService.scoreOrdersDetails(id);
    } catch (Exception e) {
      log.info("异常原因=======================>" + e);
      messageResult.setFailureResult("系统异常,请重新点击");
    }

    log.info("查询订单详情返回参数=======================>" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改订单物流信息
   *
   * @param dto 修改商品物流信息参数
   * @return 修改状态成功与否, 返回0000为成功
   */
  @RequestMapping(value = "/updateScoreOrder", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateScoreOrders(@Valid @RequestBody MallLogisticsDto dto, BindingResult bindingResult) {
    log.info("修改物流状态请求参数=======================>" + dto.toString());
    MessageResult messageResult = new MessageResult();

    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      messageResult = scoreOrderService.updateOrder(dto);
    } catch (Exception e) {
      log.info("异常原因=======================>" + e);
      messageResult.setFailureResult("系统异常");
    }

    log.info("修改物流返回参数=======================>" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查询快递公司列表以及快递状态列表
   *
   * @return 快递公司列表以及快递状态列表
   */
  @RequestMapping(value = "/queryLogisticsList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryLogisticsList() {
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = scoreOrderService.queryLogisticsList();
    } catch (Exception e) {
      log.info("异常原因=======================>" + e);
      messageResult.setFailureResult("系统异常");
    }

    log.info("返回参数=======================>" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


}
